<?php
/**
 * Description: wwwroot
 * Created by PhpStorm.
 * User: 玉坤
 * Date: 2021/6/16
 * Time: 19:06
 */

namespace Biz\model\brandActivity;


use Doraemon\model\Phone;
use Doraemon\pockets\datebase\BaseDbModel;
use Exception;

class UserAddressModel extends BaseDbModel
{
    public const _CONFIG_FILE_ = 'db_store';

    public const _CONFIG_SELECT_ = 'db_brand_activity';

    public const _DATABASE_ = 'brand_activity';

    public const _TABLE_NAME = 'user_address';

    //分享
    public const SHARE = 1;

    public static function getPhoneScene(): int
    {
        return \hhz\constant\basic\UserExtendPhoneScene::NEW_PRODUCT_USER_ADDRESS_PHONE;
    }

    /**
     * @throws Exception
     */
    public static function encryptPhone($phone): ?int
    {
        return Phone::addExtendPhone($phone, self::getPhoneScene());
    }

    /**
     * @throws Exception
     */
    public static function decryptPhone($encrypt): ?string
    {
        return Phone::getExtendPhone($encrypt, self::getPhoneScene());
    }


    /**
     * 获取一行
     * @param $where
     * @param string $field
     * @return array|mixed|null
     */
    public static function first($where, $field = '*')
    {
        $rs = self::get($field, $where);
        if (!$rs) {
            return null;
        }

        return $rs;
    }

    /**
     * 批量查询
     * @param $where
     * @param  $field
     * @return array|bool
     */
    public static function all($where, $field)
    {
        return self::select($field, $where);
    }

    /**
     * 获取一列的值
     * @param $where
     * @param array $field
     * @return array|bool
     */
    public static function pluck($where, array $field = ['id'])
    {
        return self::select($field, $where);
    }

    /**
     * 从记录中获取单个值
     * @param $where
     * @param string $columns
     * @return array|bool
     */
    public static function value($where, string $columns = 'id')
    {
        return self::get($columns, $where);
    }

    /**
     * 连表查询
     * @param $table
     * @param $main_col
     * @param $attach_col
     * @param $field
     * @param $where
     * @return array|bool
     */
    public static function left_join($table, $main_col, $attach_col, $field, $where)
    {
        $join = ['[>]'.$table => [$main_col => $attach_col]];

        return self::select($join, $field, $where);
    }

    /**
     * @param array $ids
     * @return array
     */
    public static function getByIds(array $ids): array
    {
        if ($ids) {
            $map = [
                'id' => $ids,
            ];
            return self::select('*', $map);
        } else {
            return [];
        }

    }
}